const fs = require('fs');
const path = require('path');
const mime = require('mime-types');
const pdf = require('pdf-parse');
const { Client } = require('pg');

const client = new Client({
  host: "127.0.0.1",
  user: "postgres",
  password: "whuyiyulu666666",
  database: "animalsmigration",
  port: 5432,
});

// 注意路径必须是绝对路径或 __dirname 相对路径
const pdfDir = path.join(__dirname,'data_tobe_inserted');

// 为特定文件指定 animal_tags
const animalTagsMap = {
  "10756146.pdf": { animal1: "monkey", animal2: "rabbits" },
  "example2.pdf": { animal1: "elephant" },
  // ... 可添加更多文件名
};

async function extractPDFInfo(buffer) {
  const data = await pdf(buffer);
  const text = data.text;

  const lines = text.split('\n').map(l => l.trim()).filter(Boolean);
  const title = lines[0] || '';

  const authorMatch = text.match(/(?:Author[s]?:|作者[:：])\s*(.+)/i);
  const author = authorMatch ? authorMatch[1].trim() : '';

  const abstractMatch = text.match(/(?:Abstract|摘要)[:：]?\s*(.+?)(?:\n{2,}|\n[A-Z])/is);
  const abstract = abstractMatch ? abstractMatch[1].trim() : '';

  const journalMatch = text.match(/(?:Journal|期刊|Published in|刊名)[:：]?\s*(.+)/i);
  const journal = journalMatch ? journalMatch[1].trim() : '';

  return { title, author, abstract, journal };
}

async function importPDFs() {
  try {
    await client.connect();

    const files = fs.readdirSync(pdfDir).filter(f => f.toLowerCase().endsWith('.pdf'));

    for (const file of files) {
      const filePath = path.join(pdfDir, file);
      const fileData = fs.readFileSync(filePath);
      const mimeType = mime.lookup(filePath) || 'application/pdf';

      // 防止重复导入：按 file_name 查重
      const check = await client.query(
        `SELECT id FROM media_files WHERE file_name = $1`,
        [file]
      );
      if (check.rows.length > 0) {
        console.log(`已存在，跳过: ${file}`);
        continue;
      }

      try {
        const metadata = await extractPDFInfo(fileData);

        const animalTags = animalTagsMap[file] || null;

        const result = await client.query(`
          INSERT INTO media_files 
            (file_name, file_type, mime_type, file_data, title, author, abstract, journal, animal_tags)
          VALUES 
            ($1, 'pdf', $2, $3, $4, $5, $6, $7, $8)
          RETURNING id
        `, [
          file, mimeType, fileData,
          metadata.title, metadata.author, metadata.abstract, metadata.journal,
          animalTags
        ]);

        const insertedId = result.rows[0].id;
        console.log(`导入成功: ${file} (ID: ${insertedId})`);

      } catch (err) {
        console.error(`处理文件 ${file} 出错:`, err.message);
      }
    }
  } catch (e) {
    console.error("数据库连接失败:", e.message);
  } finally {
    await client.end();
  }
}

importPDFs();
